import { Injectable } from '@angular/core';
import { NzDrawerService } from 'ng-zorro-antd/drawer';
import { Observable } from 'rxjs';
import { IdentityRoleDto, IdentityUserDto, IdentityUserService } from '../proxy/identity';
import { RefPermissionComponent } from './ref-permission.component';

@Injectable({
  providedIn: 'root',
})
export class RefPermissionService {
  private defaultTitle: '角色权限' | '用户权限' = '角色权限';
  private defaultWidth: number = 800;
  constructor(
    protected drawerService: NzDrawerService,
    protected identityUserService: IdentityUserService
  ) {}
  show(
    model: { providerName: string; keyModel: IdentityRoleDto | IdentityUserDto },
    options?: { title?: string; width?: number }
  ): Observable<any> {
    var nzTitle = !options?.title
      ? `${model.providerName == 'R' ? '角色权限' : '用户权限'}:${
          model.providerName == 'R' ? model.keyModel.name : model.keyModel['userName']
        }`
      : options.title;
    var nzWidth = !options?.width ? this.defaultWidth : options.width;
    var providerKey = model.keyModel.name;
    if (model.providerName == 'U') providerKey = model.keyModel.id;
    const drawerRef = this.drawerService.create<RefPermissionComponent, { value: string }, string>({
      nzTitle: nzTitle,
      nzWidth: nzWidth,
      nzContent: RefPermissionComponent,
      nzContentParams: {
        providerName: model.providerName,
        providerKey: providerKey,
      },
    });
    return drawerRef.afterClose;
  }
}
